Web Hizmetindeki kişileri listeliyorum ve ekran görüntüsünde görüldüğü gibi 'bölümlü' tableView'da görüntülüyorum. Sorun şu ki, bölüm A ve bölüm S için ilk satırın onay kutuları için aynı etiket değerlerini alıyorum. Bir diziyi sıraladım ve indekslenmiş tablo görünümünde görüntüledim. IndexPath.row'a bağlı olarak, görüntülenen bölümlerin sayısına bakılmaksızın farklı etiket değerleri nasıl alınır. İşte denediğim şey CellForRowAtIndexPath içinde: UIButton * checkBox; eğer (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPhone) { checkBox = [[UIButton tahsisi] initWithFrame: CGRectMake (7, 8, 30, 30)]; } Başka { checkBox = [[UIButton tahsisi] initWithFrame: CGRectMake (15, 13, 30, 30)]; } // int cnt = 0; // for (int i = indexPath.section - 1; i> 0; i--) // { // cnt + = [[objectsForCharacters objectForKey: [arrayOfCharacters objectAtIndex: i]] count]; // arrayOfCharachters'ın karakteri "A" dan "Z" ye sahiptir //} //checkBox.tag = cnt + indexPath.row; [checkBox setImage: [UIImage imageNamed: @ "checkBox.png"] forState: UIControlStateNormal]; [onay kutusu addTarget: kendi kendine eylem: @selector (checkBoxClicked :) forControlEvents: UIControlEventTouchUpInside]; [onay kutusu setTag: indexPath.row]; [hücre.contentView addSubview: checkBox]; dönüş hücresi; } - (void) checkBoxClicked: (kimlik) gönderen { CGPoint buttonPosition = [gönderen convertPoint: CGPointZero toView: self.tableViewContact]; NSIndexPath * indexPath = [self.tableViewContact indexPathForRowAtPoint: buttonPosition]; UIButton * tappedButton = (UIButton *) gönderen; NSLog (@ "Etiket numarası =% d", [gönderen etiketi]); eğer ([tappedButton.currentImage isEqual: [UIImage imageNamed: @ "checkBox.png"]]) { [gönderen setImage: [UIImage imageNamed: @ "checkBoxMarked.png"] forState: UIControlStateNormal]; eğer (indexPath! = Nil) { NSString * finalIntId = [mutableArrayOfIds objectAtIndex: indexPath.row]; // onay kutusu kimliklerini mutableArrayOfIds içinde saklayın NSLog (@ "Etiketli işaretli düğme id =% @", finalIntId); [arrayOfIds addObject: finalIntId]; } // NSString * finalIntId = [mutableArrayOfIds objectAtIndex: tappedButton.tag]; // NSString * finalIntId = [mutableArrayOfIds objectAtIndex: indexPath.row]; } Başka { [gönderen setImage: [UIImage imageNamed: @ "checkBox.png"] forState: UIControlStateNormal]; NSLog (@ "Kontrol Edilmedi"); // [arrayOfIds removeObjectAtIndex: tappedButton.tag]; } } - (NSString *) tableView: (UITableView *) tableView titleForHeaderInSection: (NSInteger) section { eğer ([arrayOfCharacters count] == 0) { dönüş @""; } return [NSString stringWithFormat: @ "% @", [arrayOfCharacters objectAtIndex: section]]; } - (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView { NSArray * toBeReturned = [NSArray arrayWithArray: [@ "A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | # " componentsSeparatedByString: @ "|"]]; Geri Döndürüldü; } - (NSInteger) tableView: (UITableView *) tableView sectionForSectionIndexTitle: (NSString *) title atIndex: (NSInteger) indeksi { NSInteger sayısı = 0; for (arrayOfCharacters içindeki NSString * karakteri) { eğer ([character isEqualToString: title]) { dönüş sayısı; } count ++; } dönüş 0; } - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView { return [arrayOfCharacters count]; // return 1; } - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) bölümü { // return [mutableArray sayısı]; return [[objectsForCharacters objectForKey: [arrayOfCharacters objectAtIndex: section]] count]; }
2020-12-07 22:56:24
aynı satıra sahip tüm bölümler için aynı etiket değerini ayarlıyorsunuz. İndexPath'in iki özelliği vardır, {section, row}. A bölümünde iki satır olduğunu varsayalım. satır1 için -> indexPath.section = 0, indexPath.row = 0; satır2-> indexPath.section = 0 için, indexPath.row = 1; S bölümünün iki satırı olduğunu varsayalım. satır1 -> indexPath.section = 1 için, indexPath.row = 0; row2-> indexPath.section = 1 için, indexPath.row = 1; Yani, bölüm A'nın 1. satırı ve S bölümünün 1. satırı için aynı etiket değerini ayarlıyorsunuz, bu da 0'dır. Etiket değerini aşağıdaki gibi ayarlamayı deneyin. button.tag = indexPath.section * 1000 + indexPath.row; bölümü ve satırı alırken, NSInteger bölümü = (button.tag) / 1000; NSInteger satır = (button.tag)% 1000; | Bunu dene... - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { statik NSString * CellIdentifier = @ "Cell"; UITableViewCell * hücre = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; eğer (hücre == nil) { hücre = [[UITableViewCell tahsis] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; } UIButton * checkBox = [[UIButton tahsisi] initWithFrame: CGRectMake (280, 10, 50, 44)]; checkBox.backgroundColor = [UIColor orangeColor]; [onay kutusu addTarget: kendi kendine eylem: @selector (checkBoxClicked: event:) forControlEvents: UIControlEventTouchUpInside]; [onay kutusu setTag: indexPath.row]; [hücre.contentView addSubview: checkBox]; dönüş hücresi; } - (void) checkBoxClicked: (id) gönderen olayı: (id) olay { NSSet * touches = [olay allTouches]; UITouch * touch = [herhangi birNesne'ye dokunur]; CGPoint currentTouchPosition = [locationInView'a dokunun: self.tv]; // burada tv TableView Nesnesi NSIndexPath * indexPath = [self.tv indexPathForRowAtPoint: currentTouchPosition]; NSLog (@ "indePath.section değeri% d, indexPath.row% d", indexPath.section, indexPath.row); } | Bu, BAĞIMSIZ bölümlerdeki düğmelere etiket atadığınız için oluyor. A ve S Bölümlerinin İlk Satırının her ikisi de satır = 0'a sahiptir, bu nedenle ilgili düğmelerine Atanan Etiket 0'dır. Bölümlerinize referans vererek onları atamalısınız. Section, Row içeren virgülle ayrılmış form ile erişilebilirlik ipucu atamanızı öneririm. Ve senin yönteminde - (void) checkBoxClicked: (kimlik) gönderen { // virgülle ayrılmış formdaki dizeyi seçin. 1. sizin bölümünüz, 2. satır. } İkinci seçenek, ne yaparsanız yapın ve Button yönteminizi bu şekilde uygulayın. CGPoint buttonPosition = [gönderen convertPoint: CGPointZero toView: self.tableView]; NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: buttonPosition]; eğer (indexPath! = nil) { ... indexpath.section, bölümünüz, index path.row ise satırınızdır. } Üçüncü seçenek de var. cellforRowAtIndexpath içinde Button öğenize bir başlık atayın - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath [btn setTitle: <# Satırınız #> forState: UIControlStateDisabled]; [btn setTag <# Bölümünüz #>]; Dolayısıyla, Düğme Yönteminizi Aldıktan sonra hem Bölüme (Etiket) hem de Satıra (Devre Dışı Durum Başlığı) sahip olabilirsiniz. - (void) checkBoxClicked: (id) gönderen {[button titleForState: UIControlStateDisabled]; // Satırın button.tag // senin Bölüm} | Bunu dene! Her bölüm doğru bölüm sayısını biliyor olabilir, ardından tek tek satır sayısını ekleyebilir. [[fieldTitlesList objectAtIndex: indexPath.section - 1] count] + indexPath.row FieldTitlesList, bölümlerinizin dizisidir. | Sorunumu çözen aşağıdaki kodu ekledim NSInteger rowNumber = 0; for (NSInteger i = 0; i